const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

// 格式化时间戳
function getFullTime(timestamp) {
  var time = arguments[0] || 0;
  var t, y, m, d, h, i, s;
  t = time ? new Date(time * 1000) : new Date();
  y = t.getFullYear(); // 年
  m = t.getMonth() + 1; // 月
  d = t.getDate(); // 日

  h = t.getHours(); // 时
  i = t.getMinutes(); // 分
  s = t.getSeconds(); // 秒

  return [y, m, d].map(formatNumber).join('-') + ' ' + [h, i, s].map(formatNumber).join(':');
}

// 格式化时间戳
function getTime(timestamp) {
  var time = arguments[0] || 0;
  var t, y, m, d, h, i, s;
  t = time ? new Date(time * 1000) : new Date();
  y = t.getFullYear(); // 年
  m = t.getMonth() + 1; // 月
  d = t.getDate(); // 日

  h = t.getHours(); // 时
  i = t.getMinutes(); // 分
  s = t.getSeconds(); // 秒

  // return [y, m, d].map(formatNumber).join('-') + ' ' + [h, i, s].map(formatNumber).join(':');
  return [y, m, d].map(formatNumber).join('-') + ' ' + [h, i].map(formatNumber).join(':');
}

// 格式化年月日
function getYMD(timestamp) {
  var time = arguments[0] || 0;
  var t, y, m, d, h, i, s;
  t = time ? new Date(time * 1000) : new Date();
  y = t.getFullYear(); // 年
  m = t.getMonth() + 1; // 月
  d = t.getDate(); // 日

  return [y, m, d].map(formatNumber).join('-');
}

function getDateDiff(dateTimeStamp) {
  var minute = 1000 * 60;
  var hour = minute * 60;
  var day = hour * 24;
  var halfamonth = day * 15;
  var month = day * 30;
  var year = day * 365;
  var now = new Date().getTime();
  //   console.log(now);
  var diffValue = now - dateTimeStamp;
  if (diffValue < 0) {
    //非法操作
    return '数据出错';
  }
  var yearC = diffValue / year;
  var monthC = diffValue / month;
  var weekC = diffValue / (7 * day);
  var dayC = diffValue / day;
  var hourC = diffValue / hour;
  var minC = diffValue / minute;
  var result = '';
  if (yearC >= 1) {
    result = parseInt(yearC) + '年以前';
  } else if (monthC >= 1) {
    result = parseInt(monthC) + '个月前';
  } else if (weekC >= 1) {
    result = parseInt(weekC) + '星期前';
  } else if (dayC >= 1) {
    result = parseInt(dayC) + '天前';
  } else if (hourC >= 1) {
    result = parseInt(hourC) + '小时前';
  } else if (minC >= 5) {
    result = parseInt(minC) + '分钟前';
  } else {
    result = '刚刚发表';
  }
  return result;
}

function getFormatDate(str) {

  // 拆分日期为年 月 日
  var YEAR = str.substring(0, 4),
    MONTH = str.substring(4, 6),
    DATE = str.slice(-2);

  // 拼接为 2016/10/02 可用于请求日期格式
  var dateDay = YEAR + "/" + MONTH + "/" + DATE;

  // 获取星期几
  var week = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'],
    day = new Date(dateDay).getDay();

  return {
    "dateDay": MONTH + "月" + DATE + "日 " + week[day]
  }

}

function downloadFileAndSave(url) {
  wx.downloadFile({
    url: url,
    success: function(res) {
      saveImageToPhotosAlbum(res.tempFilePath);
    }
  })
}

function getImageInfoAndSave(src) {
  wx.getImageInfo({
    src: src,
    success: function(res) {
      saveImageToPhotosAlbum(res.path);
    }
  })
}

function saveImageToPhotosAlbum(filePath) {
  wx.saveImageToPhotosAlbum({
    filePath: filePath,
    success(res) {
      // console.log("saveImg:" + res);
      wx.showModal({
        title: "保存成功！"
      });
    }
  })
}

function formatMyTime(time) {
  if (typeof time !== 'number' || time < 0) {
    return time
  }

  var hour = parseInt(time / 3600)
  time = time % 3600
  var minute = parseInt(time / 60)
  time = time % 60
  var second = time

  return ([minute, second]).map(function(n) {
    n = n.toString()
    return n[1] ? n : '0' + n
  }).join(':')
}

/**
 * 正则截取字符串
 */
function getRegex(sourceStr, regexStr) {
  var dataArray = sourceStr.match(new RegExp(regexStr));
  if (dataArray && dataArray.length > 1) {
    // console.log(dataArray[1]);
    return dataArray[1];
  } else {
    return '';
  }
}
/**
 * 数组删除指定元素
 */
function delArray(arr,val){
  var index = arr.indexOf(val);
  // console.log(arr);
  arr.splice(index,1);
  // console.log(arr);
  return arr;
}
/**
 * 数组去重
 */
function uniqueArray(arr){
  if(arr.length==0){
    return [];
  }
  arr.sort();
  var hash=[arr[0]];
  for (var i = 1; i < arr.length; i++) {
     if(arr[i]!=hash[hash.length-1]){
      hash.push(arr[i]);
     }
  }
  return hash;
}

/**
 * 查询数组对象是否已存在id
 */
function checkArrayObj(arr,id){
  var temp = false;
  for (var i = 0; i < arr.length; i++) {
    if(arr[i].id==id){
      temp = true;
      break;
    }
  }
  return temp;
}

function timeFormat(timestamp, format) {
  var time = arguments[0] || 0;
  var t = time ? new Date(time * 1000) : new Date();
  var o = {
    "M+": t.getMonth() + 1, //month 
    "d+": t.getDate(), //day 
    "h+": t.getHours(), //hour 
    "m+": t.getMinutes(), //minute 
    "s+": t.getSeconds(), //second 
    "q+": Math.floor((t.getMonth() + 3) / 3), //quarter 
    "S": t.getMilliseconds() //millisecond 
  }

  if (/(y+)/.test(format)) {
    format = format.replace(RegExp.$1, (t.getFullYear() + "").substr(4 - RegExp.$1.length));
  }

  for (var k in o) {
    if (new RegExp("(" + k + ")").test(format)) {
      format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
    }
  }
  return format;
}

function dateStrChangeTimeTamp(dateStr){
  dateStr = dateStr.substring(0,19);
  dateStr = dateStr.replace(/-/g,'/');
  var timeTamp = new Date(dateStr).getTime()/1000;
  return timeTamp;
}

module.exports = {
  APPID: 'wxf9c2f93b81f06b75',
  YUMID: 0,
  tmplID: '',
  Version: 'v1.0.0',
  themeColor: '#0495e6',
  formatTime: formatTime,
  formatMyTime: formatMyTime,
  getTime: getTime,
  getFullTime:getFullTime,
  timeFormat:timeFormat,
  getYMD: getYMD,
  getDateDiff: getDateDiff,
  getFormatDate: getFormatDate,
  downloadFileAndSave: downloadFileAndSave,
  getImageInfoAndSave: getImageInfoAndSave,
  getRegex: getRegex,
  uniqueArray:uniqueArray,
  checkArrayObj:checkArrayObj,
  delArray:delArray,
  dateStrChangeTimeTamp:dateStrChangeTimeTamp
}